package com.shundr.auth.user.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shundr.auth.R;
import com.shundr.auth.user.model.AuthCompanyInfo;
import com.shundr.auth.user.model.UserInfo;

/**
 * 
 * AuthItemAdapter 认证列表适配器
 * 
 * 
 * @author larry 2015-4-13 下午4:26:47
 * @version 1.0.0
 *
 */
public class AuthUserItemAdapter extends BaseAdapter {
	public List<UserInfo> mList;
	private Context mContext;

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int index) {
		return mList.get(index);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	public AuthUserItemAdapter(List<UserInfo> mlist, Context mContext) {
		super();
		this.mList = mlist;
		this.mContext = mContext;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		ViewHold viewHold = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_user_info, null);
			viewHold = new ViewHold();
			viewHold.tvName = (TextView) convertView.findViewById(R.id.tv_name);
			viewHold.tvManager = (TextView) convertView
					.findViewById(R.id.tv_manager);
			viewHold.tvStatus = (TextView) convertView
					.findViewById(R.id.tv_status);
			viewHold.tvNum = (TextView) convertView.findViewById(R.id.tv_num);
			convertView.setTag(viewHold);
		} else {
			viewHold = (ViewHold) convertView.getTag();
		}
		try {
			UserInfo info = mList.get(position);

			viewHold.tvName.setText(info.getUserPhone() + "        " 
			+ (info.getUserName() == null ? "" : info.getUserName()));
			
//			String quality = "普通用户";
//			if (info.getUserQualityStaff() == 1) {
//				quality = "优质用户";
//			}
			
			viewHold.tvManager.setText(info.userRegesiterTime.substring(0, 10) + "  " + getReason(info));
			viewHold.tvStatus.setText(getStatus(info));
			viewHold.tvNum.setText(String.valueOf(position + 1));
			
			if (position > 100) {
				viewHold.tvNum.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
			} else {
				viewHold.tvNum.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}
	
	private String getReason(UserInfo info) {
		if (info.getIdValidationStatus() == 3) {
			return info.getIdValidationMsg();
		}
		
		if (info.getCompanyValidationStatus() == 3) {
			return info.getCompanyValidationMsg();
		}
		
		return "";
	}

	private String getStatus(UserInfo info) {
		String status = null;
		if (info.userType == 0) {
			// 司机
			switch (info.idValidationStatus) {
			case 0:
				status = "司机：未认证";
				break;
			case 1:
				status = "司机：待审核";
				break;
			case 2:
				status = "司机：已审核";
				break;
			case 3:
				status = "司机：被驳回";
				break;

			default:
				break;
			}
		} else {
			// 货主
			StringBuilder sb = new StringBuilder("个人：");

			switch (info.idValidationStatus) {
			case 0:
				sb.append("未认证");
				break;
			case 1:
				sb.append("待审核");
				break;
			case 2:
				sb.append("已审核");
				break;
			case 3:
				sb.append("被驳回");
				break;

			default:
				break;
			}

			sb.append("\n企业：");

			switch (info.companyValidationStatus) {
			case 0:
				sb.append("未认证");
				break;
			case 1:
				sb.append("待审核");
				break;
			case 2:
				sb.append("已审核");
				break;
			case 3:
				sb.append("被驳回");
				break;

			default:
				break;
			}

			status = sb.toString();
		}

		return status;
	}

	static class ViewHold {
		TextView tvNum;
		TextView tvName;
		TextView tvManager;
		TextView tvStatus;
	}
}
